import javax.swing.*;
import java.util.*;

/** This class creates the Action Box for a Proposed Synonym in the
 * {@link DecisionFrame} whenever the suggestion chosen by User is a
 * ProposedDef.
 *
 * @author  Gary Morris, Northern Virginia Community College		garymorris2245@verizon.net
 *
 * Created on Feb 23, 2011, 3:21:57 PM
 */
public class ActionSynonym extends javax.swing.JPanel {

    // Logical variable declarations. GUI declarations at end of file.
    DecisionFrame papa;
    TreeMap<String, ArrayList<Context.HistoryItem>> learningHistory;
    SynonymCandidate syn;
    int suggNmbr;
    boolean switchTerms = false;
    DomainTheory dt = null;  // set externally by DecisionFrame

    /** Creates new form ActionSynonym */
    public ActionSynonym() {
        initComponents();
    }

     /** Creates new form ActionSynonym */
    public ActionSynonym(DecisionFrame frame) {
        initComponents();
        papa = frame;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        actionBtnGrp = new javax.swing.ButtonGroup();
        acceptBtn = new javax.swing.JRadioButton();
        rejectBtn = new javax.swing.JRadioButton();
        noActionBtn = new javax.swing.JRadioButton();
        primeTermLabel = new javax.swing.JLabel();
        primaryTermComboBox = new javax.swing.JComboBox();
        notesScrollPane = new javax.swing.JScrollPane();
        notesTextPane = new javax.swing.JTextPane();
        doneButton = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Action Box: Synonym "));
        setPreferredSize(new java.awt.Dimension(425, 300));

        actionBtnGrp.add(acceptBtn);
        acceptBtn.setText("Accept Synonym");
        acceptBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                acceptBtnActionPerformed(evt);
            }
        });

        actionBtnGrp.add(rejectBtn);
        rejectBtn.setText("Reject Synonym");
        rejectBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rejectBtnActionPerformed(evt);
            }
        });

        actionBtnGrp.add(noActionBtn);
        noActionBtn.setSelected(true);
        noActionBtn.setText("No Action");

        primeTermLabel.setText("Primary Term is: ");

        primaryTermComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2" }));
        primaryTermComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                primaryTermComboBoxActionPerformed(evt);
            }
        });

        notesScrollPane.setBorder(javax.swing.BorderFactory.createTitledBorder("Notes: "));
        notesScrollPane.setViewportView(notesTextPane);

        doneButton.setText("Done");
        doneButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                doneButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(acceptBtn)
                            .add(rejectBtn)
                            .add(noActionBtn)))
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(primeTermLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(primaryTermComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 187, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(44, 44, 44)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(notesScrollPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 323, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(doneButton))))
                .addContainerGap(43, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(acceptBtn)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(rejectBtn)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(noActionBtn)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(primeTermLabel)
                    .add(primaryTermComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(notesScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE)
                .add(18, 18, 18)
                .add(doneButton)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    public void load(SynonymCandidate sc, int nmbr) {
        syn = sc;
        suggNmbr = nmbr;
        if (dt.addressTerms) {
            learningHistory = dt.ctxt.learningHistoryAdr;
        }else {
            learningHistory = dt.ctxt.learningHistoryRef;
        }
        // make drop-down menu of primary term
        primaryTermComboBox.setModel(new DefaultComboBoxModel(new String[]
                { syn.kinTerm, syn.otherTerm() }));
        primaryTermComboBox.setSelectedIndex(0);
        noActionBtn.setSelected(true);
        notesTextPane.setText("");
    }



    private void rejectBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rejectBtnActionPerformed
        // No action; the button serves as a switch
    }//GEN-LAST:event_rejectBtnActionPerformed

    private void acceptBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_acceptBtnActionPerformed
        // No action; the button serves as a switch
    }//GEN-LAST:event_acceptBtnActionPerformed

    private void primaryTermComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_primaryTermComboBoxActionPerformed
        switchTerms = primaryTermComboBox.getSelectedIndex() == 1;
        
    }//GEN-LAST:event_primaryTermComboBoxActionPerformed

    private void doneButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doneButtonActionPerformed
        Context.HistoryItem item;
        if (acceptBtn.isSelected()) {
            item = new Context.AcceptedSynonym(syn.kinTerm,
                    UDate.today(), "false",
                    FamilyPanel.convertBannedCharacters(notesTextPane.getText()),
                    syn.otherTerm());
            if (switchTerms) {
                syn.kinTerm = syn.otherTerm();                
            }
            if (dt.synonyms == null) {
                dt.synonyms = new TreeMap<String, String>();
            }
            dt.synonyms.put(syn.otherTerm(), syn.kinTerm);
        }else if (rejectBtn.isSelected()) {
            String pair, term1, term2, other = syn.otherTerm();
            item = new Context.RejectedSynonym(syn.kinTerm,
                    UDate.today(), "false",
                    FamilyPanel.convertBannedCharacters(notesTextPane.getText()),
                    other);
            if (syn.kinTerm.compareTo(other) < 0) {
                term1 = syn.kinTerm;
                term2 = other;
            }else {
                term2 = syn.kinTerm;
                term1 = other;
            }
            pair = term1 + "/" + term2;
            if (dt.nonSynonyms ==  null) {
                dt.nonSynonyms = new ArrayList<Object>();
            }
            dt.nonSynonyms.add(pair);
        }else return;
        item.postToHistory(learningHistory);
        papa.markProcessed(suggNmbr);
        papa.reset();
    }//GEN-LAST:event_doneButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton acceptBtn;
    private javax.swing.ButtonGroup actionBtnGrp;
    private javax.swing.JButton doneButton;
    private javax.swing.JRadioButton noActionBtn;
    private javax.swing.JScrollPane notesScrollPane;
    private javax.swing.JTextPane notesTextPane;
    private javax.swing.JComboBox primaryTermComboBox;
    private javax.swing.JLabel primeTermLabel;
    private javax.swing.JRadioButton rejectBtn;
    // End of variables declaration//GEN-END:variables

}
